#include "gm_sc_cont_mgr.h"
#include <memory.h>
#include <string.h>

gm_sc_cont_mgr::gm_sc_cont_mgr()
{
}

gm_sc_cont_mgr::~gm_sc_cont_mgr()
{
	clear();
}

ULONG gm_sc_cont_mgr::add_cont( gm_sc_cont * cont )
{
	return _sc_con_list.add(cont);
}

ULONG gm_sc_cont_mgr::remove_cont(char *pName)
{
	gm_sc_cont * pcont = get_cont_by_name(pName);
	while(pcont != NULL)
	{
		delete pcont;
		_sc_con_list.remove(pcont);	
		pcont = get_cont_by_name(pName);
	}
	return SAR_OK;
}

ULONG gm_sc_cont_mgr::remove_cont(gm_sc_cont *pcont)
{
	ULONG lRslt = SAR_OK;
	if(pcont != NULL)
	{
		delete pcont;
		_sc_con_list.remove(pcont);	
	}
	return lRslt;
}



gm_sc_cont * gm_sc_cont_mgr::get_cont_by_name( char *pName )
{
	ULONG lRslt = SAR_OK;

	mk_node* pHead = _sc_con_list.get_head();
	if(pHead == NULL) return NULL;
	gm_sc_cont * pcon = (gm_sc_cont *)pHead->get_data();

	if(strcmp(pcon->Name(), pName) == 0)
		return pcon;

	mk_node* pNode = pHead->get_next_node();
	while(pNode != NULL)
	{
		pcon = (gm_sc_cont *)pNode->get_data();
		if(strcmp(pcon->Name(), pName) == 0)
			return pcon;

		pNode = pNode->get_next_node();
	}
	return NULL;
}

ULONG gm_sc_cont_mgr::clear()
{
	ULONG lRslt = SAR_OK;
	mk_node* pHead = _sc_con_list.get_head();
	while (pHead != NULL)
	{
		mk_node *node = pHead;
		
		gm_sc_cont *pcont = (gm_sc_cont *)node->get_data();
		delete pcont;

		pHead = node->get_next_node();
	}
	_sc_con_list.remove_all();
	return lRslt;
}

gm_sc_cont* gm_sc_cont_mgr::get_first_cont()
{
	_curr_pos = _sc_con_list.get_head();
	if(_curr_pos == NULL)
	{
		return NULL;
	}
	return (gm_sc_cont *)_curr_pos->get_data();
}

gm_sc_cont* gm_sc_cont_mgr::get_next_cont()
{
	if(_curr_pos == NULL)
	{
		_curr_pos =  _sc_con_list.get_head();
	}

	if(_curr_pos == NULL)
	{
		return NULL;
	}

	_curr_pos = _curr_pos->get_next_node();
	if(_curr_pos == NULL)
	{
		return NULL;
	}

	return (gm_sc_cont *)_curr_pos->get_data();
}